package com.well.register.netty;

import com.well.register.config.PropertiesConfig;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import lombok.Data;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.SSLException;
import java.io.File;

/**
 * <p>
 * ssl context config
 * </p>
 *
 * @author weedo
 * @since 2022/8/24
 */

@Data
public class ServerSsl {

    private PropertiesConfig propertiesConfig;

    public ServerSsl(PropertiesConfig propertiesConfig) {
        this.propertiesConfig = propertiesConfig;
    }

    public SslContext getSslContext() {
        File serverCert = new File(propertiesConfig.getCertPath());
        File serverKey = new File(propertiesConfig.getKeyPath());
        File rootFile = new File(propertiesConfig.getCaPath());
        SslContext sslCtx = null;
        try {
            sslCtx = SslContextBuilder.forServer(serverCert, serverKey).trustManager(rootFile).clientAuth(ClientAuth.REQUIRE).build();
        } catch (SSLException e) {
            e.printStackTrace();
        }
        return sslCtx;
    }


}
